/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Bean.EmployeeBean;
import Bean.RoleBean;
import Utility.DBProcess;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Vietlt
 */
public class RoleDAO {

    public static void main(String[] args) {
//        LoadAll
        List<RoleBean> listRole = new RoleDAO().LoadAllRole();
        for (RoleBean role : listRole) {
            System.out.println("Role Name :" + role.getRoleName());
        }
        //LoadById
        RoleBean roleB = new RoleDAO().LoadAllRoleById("Administrator");
        System.out.println("Role Name :" + roleB.getRoleName());
    }

    public List<RoleBean> LoadAllRole() {
        List<RoleBean> list = new ArrayList<RoleBean>();
        Connection conn = null;
        CallableStatement stm = null;
        ResultSet rs = null;
        conn = DBProcess.getConnection();
        try {
            stm = conn.prepareCall("{ call Role_getall}");
            rs = stm.executeQuery();
            while (rs.next()) {
                RoleBean roleB = new RoleBean();
                roleB.setRoleName(rs.getString("RoleName"));
                list.add(roleB);
                if (list.isEmpty()) {
                    String err = "List empty !!!";
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(RoleDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DBProcess.closeResultSet(rs);
            DBProcess.closeStatement(stm);
            DBProcess.closeConnection(conn);
        }
        return list;
    }

    public RoleBean LoadAllRoleById(String roleName) {
        Connection conn = null;
        CallableStatement stm = null;
        ResultSet rs = null;
        conn = DBProcess.getConnection();
        RoleBean roleB = new RoleBean();
        try {
            stm = conn.prepareCall("{ call Role_get (?)}");
            stm.setString(1, roleName);
            rs = stm.executeQuery();
            while (rs.next()) {
                roleB.setRoleName(rs.getString("RoleName"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ApplicantDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DBProcess.closeResultSet(rs);
            DBProcess.closeStatement(stm);
            DBProcess.closeConnection(conn);
        }
        return roleB;
    }
}
